class Admin::MagazinesController < ApplicationController
  before_filter :authorize_admin
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @magazine_pages, @magazines = paginate :magazines, :per_page => 10
  end

  def show
    @magazine = Magazine.find(params[:id])
  end

  def new
    @magazine = Magazine.new
    @categories = Category.find(:all, :order => :name).map {|c| [c.name, c.id]}
  end

  def create
    @magazine = Magazine.new(params[:magazine])
    if @magazine.save
      flash[:notice] = 'Revista cadastrada com sucesso!<br/><br/>'
      redirect_to :action => 'list'
    else
      redirect_to :action => 'new'
      flash[:notice] = 'Revista nao cadastrada!<br/><br/>'
    end
  end

  def edit
    @magazine = Magazine.find(params[:id])
    @categories = Category.find(:all, :order => :name).map {|c| [c.name, c.id]}
  end

  def update
    @magazine = Magazine.find(params[:id])
    if @magazine.update_attributes(params[:magazine])
      flash[:notice] = 'Dados de revista atualizados com sucesso!<br/><br/>'
      redirect_to :action => 'show', :id => @magazine
    else
      redirect_to :action => 'edit', :id => @magazine
      flash[:notice] = 'Revista nao cadastrada!<br/><br/>'
    end
  end

  def destroy
    Magazine.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end
